package bb.lanxing.adapter;

import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.collection.LongSparseArray;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import bb.lanxing.R;
import bb.lanxing.databinding.ItemDeviceSyncFileBinding;
import bb.lanxing.databinding.sprint.DeviceSyncItem;
import bb.lanxing.lib.devices.core.sync.DeviceFile;
import bb.lanxing.lib.devices.core.sync.SyncManager;
import java.util.Iterator;
import java.util.List;

public class DeviceSyncAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnCreateContextMenuListener {
    protected List<DeviceFile> deviceFiles;
    private EventListener eventListener;
    protected LongSparseArray<DeviceSyncItem> syncItems;
    protected SyncManager syncManager;

    public interface EventListener {
        void onSync(DeviceFile deviceFile);
    }

    public void setEventListener(EventListener eventListener) {
        this.eventListener = eventListener;
    }

    public void setDeviceFiles(List<DeviceFile> list) {
        this.deviceFiles = list;
        LongSparseArray<DeviceSyncItem> longSparseArray = this.syncItems;
        if (longSparseArray != null) {
            longSparseArray.clear();
        }
        this.syncItems = new LongSparseArray<>();
        notifyDataSetChanged();
    }

    public List<DeviceFile> getDeviceFiles() {
        return this.deviceFiles;
    }

    public void setSyncManager(SyncManager syncManager) {
        this.syncManager = syncManager;
    }

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
        new MenuInflater(view.getContext()).inflate(R.menu.menu_device_sync_item, contextMenu);
    }

    public void delete(long j) {
        Iterator<DeviceFile> it = this.deviceFiles.iterator();
        int i = 0;
        while (it.hasNext()) {
            if (it.next().getId() == j) {
                it.remove();
                notifyItemRemoved(i);
            }
            i++;
        }
    }

    public DeviceSyncItem getDeviceSyncItem(long j) {
        LongSparseArray<DeviceSyncItem> longSparseArray = this.syncItems;
        if (longSparseArray != null) {
            return longSparseArray.get(j);
        }
        return null;
    }

    public DeviceFile getDeviceFileByPosition(int i) {
        List<DeviceFile> list = this.deviceFiles;
        if (list == null || i >= list.size()) {
            return null;
        }
        return this.deviceFiles.get(i);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        DeviceSyncViewHolder deviceSyncViewHolder = new DeviceSyncViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_device_sync_file, viewGroup, false));
        deviceSyncViewHolder.itemView.setOnCreateContextMenuListener(this);
        return deviceSyncViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        DeviceSyncViewHolder deviceSyncViewHolder = (DeviceSyncViewHolder) viewHolder;
        final DeviceFile deviceFile = this.deviceFiles.get(i);
        DeviceSyncItem deviceSyncItem = this.syncItems.get(deviceFile.getId());
        if (deviceSyncItem == null) {
            deviceSyncItem = makeSyncItem(deviceFile);
            this.syncItems.put(deviceFile.getId(), deviceSyncItem);
        }
        deviceSyncItem.reset();
        deviceSyncItem.setDeviceFile(deviceFile);
        SyncManager syncManager = this.syncManager;
        deviceSyncItem.setStatus(syncManager != null ? syncManager.getSyncState(deviceFile.getId()) : -1);
        ItemDeviceSyncFileBinding binding = deviceSyncViewHolder.getBinding();
        binding.tvStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (DeviceSyncAdapter.this.eventListener != null) {
                    DeviceSyncAdapter.this.eventListener.onSync(deviceFile);
                }
            }
        });
        //TODO:hu
//        binding.setSyncItem(deviceSyncItem);
        binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        List<DeviceFile> list = this.deviceFiles;
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    protected DeviceSyncItem makeSyncItem(DeviceFile deviceFile) {
        return new DeviceSyncItem();
    }

    public class DeviceSyncViewHolder extends RecyclerView.ViewHolder {
        ItemDeviceSyncFileBinding binding;

        public DeviceSyncViewHolder(View view) {
            super(view);
            this.binding = (ItemDeviceSyncFileBinding) DataBindingUtil.bind(view);
        }

        public ItemDeviceSyncFileBinding getBinding() {
            return this.binding;
        }
    }
}
